package top.codedance.iotp.iot.sdk.po.async;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.lang.reflect.Field;

public class AsyncBaseParam {
    private String username;
    private String password;
    private String deviceId;

    public AsyncBaseParam(){}

    public AsyncBaseParam(String username, String password, String deviceId) {
        this.username = username;
        this.password = password;
        this.deviceId = deviceId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    @Override
    public String toString(){
        Field[] declaredFields = getClass().getDeclaredFields();
        JSONObject jsonObject = new JSONObject();
        for (Field f : declaredFields){
            f.setAccessible(true);
            try {
                jsonObject.put(f.getName(), f.get(AsyncBaseParam.this));
            }catch (Exception e){}
        }
        loopGetParentFields(getClass(), jsonObject);
        return JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
    }

    protected void loopGetParentFields(Class<?> clazz, JSONObject jsonObject){
        Class<?> superclass = clazz.getSuperclass();
        if(superclass != null) {
            Field[] declaredFields1 = superclass.getDeclaredFields();
            for (Field f : declaredFields1) {
                f.setAccessible(true);
                try {
                    jsonObject.put(f.getName(), f.get(AsyncBaseParam.this));
                } catch (Exception e) {
                }
            }
            loopGetParentFields(superclass, jsonObject);
        }
    }
}
